package com.kstech.ks_gaz01.realTime

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.blankii.androidlibrary.utils.CalcUtil
import com.chad.library.adapter4.BaseQuickAdapter
import com.kstech.ks_gaz01.data.DebugDataBean
import com.kstech.ks_gaz01.databinding.LayoutItemRealtimeBinding

class RealTimeAdapter : BaseQuickAdapter<DebugDataBean.Item, RealTimeAdapter.VH>() {
    // 自定义ViewHolder类
    class VH(
        parent: ViewGroup,
        val binding: LayoutItemRealtimeBinding = LayoutItemRealtimeBinding.inflate(
            LayoutInflater.from(parent.context), parent, false
        ),
    ) : RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): VH {
        // 返回一个 ViewHolder
        return VH(parent)
    }

    override fun onBindViewHolder(
        holder: VH,
        position: Int,
        item: DebugDataBean.Item?
    ) {

        holder.binding.itemName.text = item!!.para1
        holder.binding.unit.text = item.para5
        holder.binding.address.text = item.para0

        if (item!!.para0.contains("_")) {
            val binaryString = CalcUtil.Hex2BinaryString(item.revData)//十六进制
            holder.binding.value.text = binaryString[item.bitPosition].toString()
        } else {
            holder.binding.value.text = CalcUtil.Hex2IntStr(item.revData, item.isNegativeNumber)
        }
    }
}